<script type="text/javascript">
	var form_url = '/weixin/keyword/add' ;
	$(function() {
		var news = $('#news'), text = $('#text'), video = $('#video'), fileupload ;
		var content = $('input[name=content]').textbox({multiline:true});
		
		var newsCategory = $('#newsCategroy').combogrid({
			method: 'GET',
            idField:'id', textField:'category', panelWidth:450, rownumberWidth: 40,
            nowrap: true, striped: true, singleSelect: false,
			checkbox: true, pagination: true, pageSize: 30, pageList: [30,40,50,100,200,500,1000],
			autoRowHeight: true, border: false, rownumbers: true,
			remoteSort: true, sortName: 'id', sortOrder: 'desc', queryParams: {},
            columns: [[
				{title: 'ID', field: 'id', hidden: true},
				{title: '类别名称', field: 'category', width: 230},
				{title: '状态', field: 'status', width: 70, align: 'center', formatter: function(value, row, index) {
					if(value==0) return '禁用';
					else if(value==1) return '正常';
					else return '-';
				}},
				{title: '排序', field: 'orderNum', width: 50}
			]]
		});
		
		fileupload = $('#fileupload').fileupload({
			url: '/weixin/news/upload',
			autoUpload: true, acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
			previewCrop: true, previewMaxWidth: 150, previewMaxHeight: 80,
	    }).on('fileuploadprocessalways', function (e, data) {
	    	var index = data.index, file = data.files[index], node = $('.preview');
	        if (file.preview) {
	            node.html(file.preview);
	        }
	    }).on('fileuploadprogressall', function (e, data) { //上传进度
	        var progress = parseInt(data.loaded / data.total * 100, 10);
	    	$('#progress').html('<img src="/static/images/loading.gif" width="13" height="13">');
	    }).on('fileuploaddone', function (e, data) { //上传完成
	    	$('#progress').html('<span style="color: green;">成功：'+data.files[0].name+'</span>');
	    	$('input[name=url]').val(data.result.message);
	    }).on('fileuploadfail', function (e, data) { //上传失败
	    	$('#progress').html('<span style="color: red;">失败：'+data.files[0].name+'</span>');
	    });
		
		$('#responseType').combobox({
			method: 'get', url:'/weixin/dict/response_type_commonbox',
			editable: false, panelHeight:'auto', textField:'name', valueField:'value',
			onChange: function(newValue, oldValue){
	            if(newValue == "text") {
	            	text.show(); news.hide(); video.hide();
	            	content.textbox('enable');
	            	newsCategory.combogrid('disable');
	            	
	            } else if(newValue == "news") {
	            	text.hide(); news.show(); video.hide();
	            	content.textbox('disable');
	            	newsCategory.combogrid({url: '/weixin/news/category/list', disabled: false, editable: false});
	            	
	            } else if(newValue == "video") {
	            	text.hide(); news.hide(); video.show();
	            }
			}
		});
		
		if(null != "" && "${id!}" != "") {
			form_url = '/weixin/keyword/update' ;
			$.get('/weixin/keyword/get/${id!}', function(result) {
				try {
					$('#form').form('load', result);
				} catch(e) {
					$.easyui.loaded(); console.error(e) ;
				}
			}, 'JSON').complete(function(){$.easyui.loaded();});
		}		
	});
	
	function submitForm() {
		var checkform = $('#form').form('validate');
		if(checkform) {
			$.easyui.loading();
			$.easyui.loaded();
			var data = MM.getData('#form') ;
			if(data.responseType == "news") {
				data['responseParams'] = "{\"categoryId\": \""+data.newsCategory+"\"}"
			} else if(data.responseType == "video") {
				data['responseParams'] = "{\"videoUrl\": \""+data.url+"\"}"
			}
			$.post(form_url, data, function(result) {
				try {
					if(result.code == 200) {
						$obj.reload();$window.window('close');alertify.success(result.message);
					} else {
						$.messager.alert('提示信息', result.message, 'warning') ;
					}
				} catch(e) {
					$.easyui.loaded(); console.error(e) ;
				}
			}, 'JSON').complete(function(){$.easyui.loaded();});
		}
	}
</script>

<div class="easyui-panel form-panel" data-options="border: false, fit: true">
	<form id="form" method="post">
		<input type="hidden" name="id" value="${id!}">
		<table class="table-form">
			<tr>
				<th style="width: 100px;">关键字：</th>
				<td><input class="easyui-textbox" name="keywords" style="width:180px" data-options="required:true,validateOnCreate:false,validateOnBlur:true"></td>
			</tr>
			<tr>
				<th>状态：</th>
				<td>
					<select class="easyui-combobox" name="status" style="width:180px;" data-options="required:true, editable: false, panelHeight:'auto'">
		                <option value="1">正常</option>
		                <option value="0">失效</option>
		            </select>
				</td>
			</tr>
			<tr>
				<th>响应类型：</th>
				<td><input id="responseType" class="easyui-textbox" name="responseType" style="width:180px" data-options="required:true,validateOnCreate:false,validateOnBlur:true"></td>
			</tr>
			<tr id="news" style="display: none;">
				<th>图文类别：</th>
				<td><input id="newsCategroy" name="newsCategory" style="width:180px"></td>
			</tr>
			<tr id="text" style="display: none;">
				<th>文本信息：</th>
				<td colspan="3">
					<input name="content" style="width:350px;height:60px">
				</td>
			</tr>
			<tr id="video" style="display: none;">
				<th>上传视频：</th>
				<td colspan="3" style="position: relative;">
					<span class="btn btn-success fileinput-button">
	                    <span>选择文件</span>
	                    <input id="fileupload" type="file" name="file" multiple>
	                    <input type="hidden" name="url">
	                </span>
	                <span id="progress" style="position: absolute; left: 75px; top: 7px;width: 300px;height: 16px;overflow: hidden;"></span>
				</td>
			</tr>
			
		</table>
	</form>
</div>

<div class="submit-toolbar" id="footer"  style="border-bottom: 0;border-left: 0;border-right: 0;">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="submitForm()" style="width:80px">提交</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="MM.closeCurrentWindow()" style="width:80px">关闭</a>
</div>
